/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
import ptd.Main;

/*
 * PTDSuiteThread.java
 *
 * Created on December 15, 2005, 9:15 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

/**
 *
 * @author John
 */
public class PTDSuiteThread extends java.lang.Thread{
    
    /** Creates a new instance of PTDSuiteThread */
    public PTDSuiteThread() {
    }
    
        // -----------------------------------------------------
    public void run(){
        
        Main.debug_out("Starting script thread...");
/*        
        try{
            Runtime aRunTime = Runtime.getRuntime();
            if(ClientProc != null){
                ClientProc.destroy();
                ClientProc = null;
            }
            Main.debug_out("Executing Client...");
            File F = new File(ClientDir);
            String Exec = ClientDir + ClientApp;
            Exec = Exec + " " + Host + " " + Port;
            if(bPrompt){
                Exec = Exec + " " + 1;
            }else{
                Exec = Exec + " " + 0;
            }
            ClientProc = aRunTime.exec(Exec, null, F);
        }catch(Exception e){
            pW.reportFault(0, "Could not load client");
            return;
        }

        SSI_Socket aSock = new SSI_Socket();
        if(aSock.Connect(Host, Port) == 0){
            pW.reportFault(0, "Could not connect");
        }

        SSI_SockCmd aCmd = new SSI_SockCmd();
        SSI_SockCmd Ret = new SSI_SockCmd();
        aCmd.Set(SSI_Socket.SSI_OPEN_REQUEST, 0, null);
        if(aSock.SendCommand(aCmd) == 0){
            pW.reportFault(0, "ERROR*** Could not send open request");
            return;
        }

        aCmd = aSock.ReadCommand();
        if(aCmd.ID == SSI_Socket.RSP_NACK){
            pW.reportFault(0, "ERROR*** Could not log on..."); 
            return;
        }

        
        Main.debug_out("Loop started OK...");

        RunState = THREAD_RUNNING;
        while(bRun == true){
            aCmd = aSock.ReadCommand();
            if(aCmd.ID == SSI_Socket.SSI_LINE_REQUEST){

                
                Ret.Set(SSI_Socket.RSP_ACK, 0, null);
                aSock.SendCommand(Ret);

                LockBuffer(true);
                    pW.display(OutputBuffer);
                    OutputBuffer = "";
                LockBuffer(false);

                try{
                    while(pW.lineAvailable() == false){
                        this.sleep(100);
                    }
                }catch(Exception e){
                    Main.debug_out(e.getMessage());
                }

                
                String s = pW.getNextLine();
                if(s == null){
                    bRun = false;
                    break;
                }

                aCmd.Set(SSI_Socket.SSI_COMMAND, s.length(), s);
                aSock.SendCommand(aCmd);

                aCmd = aSock.ReadCommand();
                if(aCmd.ID == SSI_Socket.RSP_NACK){
                    pW.reportFault(0, "ERROR*** Could not log on..."); 
                    CleanUp();
                    return;
                }

            }else if(aCmd.ID == SSI_Socket.SSI_RESPONSE_OUTPUT){
                try{
                    while(bLockState == true){
                        this.sleep(1);
                    }
                }catch(Exception e){
                    Main.debug_out(e.getMessage());
                }

                LockBuffer(true);
                OutputBuffer = OutputBuffer + aCmd.Data;  // Compile Data
                LockBuffer(false);

                Ret.Set(SSI_Socket.RSP_ACK, 0, null);
                if(aSock.SendCommand(Ret) == 0){
                     CleanUp();
                     pW.reportFault(0, "ERROR*** Could not send output ack");
                     return;
                }
            }else if(aCmd.ID == SSI_Socket.SSI_BYE){
                bRun = false;

            }else{

            }
        }

        CleanUp();
        */
    }

    
}
